using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraGrid.Columns;
using MySql.Data.MySqlClient;
namespace ChuanPlus.FormControl
{
    enum ObjectType : int
    {
        Order,
        Requesting ,
        Invoice ,
        Production,
        Sample,
        ReceiptVoucher,
        Receipt ,
        Rejection 
    }
    public partial class ControlProcessData : DevExpress.XtraEditors.XtraUserControl
    {
        
        DataTable m_dtData;
        MySqlParameter[] parameters = new MySqlParameter[1];
        ArrayList m_arrData = new ArrayList();
        
        private DevExpress.XtraEditors.Repository.RepositoryItemComboBox repositoryItemComboBoStatus;
        public ControlProcessData()
        {
            InitializeComponent();
            repositoryItemComboBoStatus = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            repositoryItemComboBoStatus.Items.Add("H");
            repositoryItemComboBoStatus.Items.Add("P");
            repositoryItemComboBoStatus.Items.Add("C");
            
        }

        private void comboBoTransType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            parameters[0] = new MySqlParameter("?pStatus", MySqlDbType.VarChar);
            parameters[0].Value = "H";
            string strSql = "";
            m_arrData.Clear();  // xoa du lieu trc khi them moi

            ObjectType index = (ObjectType)comboBoTransType.SelectedIndex;
            switch (index)
            {
                case ObjectType.Order:
                    m_arrData.AddRange(DAO.OrderDAO.getOrders("H"));
                    strSql = "spGetOrderByStatus";
                    break;
                case ObjectType.Requesting:
                    m_arrData.AddRange(DAO.RequestingDAO.getReqestings("H"));
                    strSql = "spGetRequestByStatus";
                    break;
                case ObjectType.Invoice:
                    m_arrData.AddRange(DAO.InvoiceDAO.getInvoices("H"));
                    strSql = "spGetInvoiceByStatus";
                    break;
                case ObjectType.Production:
                    m_arrData.AddRange(DAO.ProductionDAO.GetProductions("H"));
                    strSql = "spGetProductionByStatus";
                    break;
                case ObjectType.Sample:
                    m_arrData.AddRange(DAO.SampleDAO.getSamples("H"));
                    strSql = "spGetSampleByStatus";
                    break;
                case ObjectType.ReceiptVoucher:
                    m_arrData.AddRange(DAO.ReceiptVoucherDAO.getReceiptVouchers("H"));
                    strSql = "spGetReceiptVoucherByStatus";
                    break;
                case ObjectType.Receipt:
                    m_arrData.AddRange(DAO.ReceiptDAO.GetReceipts("H"));
                    strSql = "spGetReceiptByStatus";
                    break;
                case ObjectType.Rejection:
                    m_arrData.AddRange(DAO.RejectionDAO.getRejections("H"));
                    strSql = "spGetRejectionByStatus";
                    break;
                default:
                    return;
            }           
            m_dtData = DataProvider.execute(strSql, parameters);
            grid.DataSource = null;
            gridView1.Columns.Clear();
            if (m_dtData.Rows.Count >= 0)
            {
                grid.DataSource = m_dtData;
                foreach (GridColumn item in gridView1.Columns)
                {
                    if (item.FieldName == "Status")
                    {
                        item.OptionsColumn.AllowEdit = true;
                        item.ColumnEdit = repositoryItemComboBoStatus;
                    }
                    else
                        item.OptionsColumn.AllowEdit = false;
                }
            }
        }
       
        private void buttonProcess_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Are you sure want to process data, after process, you can not make any change", "Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            int count = 0;
            
            if(res == DialogResult.OK)
            {                
                foreach (DevExpress.Xpo.XPLiteObject item in m_arrData)
                {                             
                    saveData(item, m_dtData.Rows[count++]["Status"] as string);
                }
                LoadData();
            }
        }
        public void reset()
        {
            m_arrData.Clear();            
            gridView1.Columns.Clear();
        }
        private void saveData(DevExpress.Xpo.XPLiteObject item, string currentStatus)
        {
            try
            {
                switch (comboBoTransType.SelectedIndex)
                {
                    case 0:
                        Order o = item as Order;
                        o.Status = currentStatus;
                        if (o.Status == "P")
                            DAO.OrderDAO.save(o);
                        else
                            o.Save();
                        break;
                    case 1:
                        Requesting req = item as Requesting;
                        req.Status = currentStatus;
                        if (req.Status == "P")
                            DAO.RequestingDAO.save(req);
                        else
                            req.Save();
                        break;
                    case 2:
                        Invoice i = item as Invoice;
                        i.Status = currentStatus;
                        if (i.Status == "P")
                            DAO.InvoiceDAO.save(i);
                        else
                            i.Save();
                        break;
                    case 3:
                        Production p = item as Production;
                        p.Status = currentStatus;
                        if (p.Status == "P")
                            DAO.ProductionDAO.save(p);
                        else
                            p.Save();
                        break;
                    case 4:
                        Sample s = item as Sample;
                        s.Status = currentStatus;
                        if (s.Status == "P")
                            DAO.SampleDAO.save(s);
                        else
                            s.Save();
                        break;
                    case 5:
                        ReceiptVoucher rv = item as ReceiptVoucher;
                        rv.Status = currentStatus;
                        if (rv.Status == "P")
                            DAO.ReceiptVoucherDAO.save(rv);
                        else
                            rv.Save();
                        break;
                    case 6:
                        Receipt r = item as Receipt;
                        r.Status = currentStatus;
                        if (r.Status == "P")
                            DAO.ReceiptDAO.save(r);
                        else
                            r.Save();
                        break;
                    case 7:
                        Rejection rej = item as Rejection;
                        rej.Status = currentStatus;
                        if (rej.Status == "P")
                            DAO.RejectionDAO.save(rej);
                        else
                            rej.Save();
                        break;
                    default:
                        return;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }
}
